package tv.danmaku.ijk.app.wdget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import tv.danmaku.ijk.media.player.R;


public class ControlPlayView extends FrameLayout {

    private boolean showControl = false;  // 是否显示控制器
    private PLAY_STATUS play_status = PLAY_STATUS.CACHING; // 播放状态

    private ImageView bg_img;  // 加载背景图

    private LinearLayout hit_Layout;  // 中间提示控件
    private ImageView loading_img;
    private Animation loadingAnimation;
    private boolean animationRunning = false;

    private LinearLayout option_Top;
    private FrameLayout option_Botton;

    private ViewControllerListener viewControllerListener;

    enum PLAY_STATUS {
        CACHING,  // 缓存状态
        PLAYING,  //  播放状态
        STOP,   // 停止状态
        ERROR,   // 异常状态
    }

    public ControlPlayView (@NonNull Context context) {
        super(context);
        init();
    }

    public ControlPlayView (@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ControlPlayView (@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init () {

        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();


        FrameLayout.LayoutParams params = null;
        LinearLayout.LayoutParams lParams = null;

        // 加载背景图
        bg_img = new ImageView(getContext());
        params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        bg_img.setLayoutParams(params);
        bg_img.setBackgroundResource(R.drawable.live_bg);
        addView(bg_img);

        // 中间提示控件
        hit_Layout = new LinearLayout(getContext());
        hit_Layout.setBackgroundColor(Color.argb(80, 43, 43, 43));
        hit_Layout.setVerticalGravity(LinearLayout.VERTICAL);
        params = new FrameLayout.LayoutParams((int) (50 * metrics.density), (int) (50 * metrics.density));
        params.gravity = Gravity.CENTER;
        hit_Layout.setLayoutParams(params);
        addView(hit_Layout);

        loading_img = new ImageView(getContext());
        loading_img.setBackgroundResource(R.drawable.loading);
        lParams = new LinearLayout.LayoutParams((int) (40 * metrics.density), (int) (40 * metrics.density));
        lParams.gravity = Gravity.CENTER_HORIZONTAL;
        hit_Layout.addView(loading_img);

        TextView hit_Text = new TextView(getContext());
        hit_Text.setText("加载中");
        lParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        hit_Text.setLayoutParams(lParams);

        hit_Layout.addView(hit_Text);


        //顶部控制条
        option_Top = new LinearLayout(getContext());
        option_Top.setBackgroundColor(Color.argb(80, 43, 43, 43));
        option_Top.setVerticalGravity(LinearLayout.HORIZONTAL);
        params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (30 * metrics.density));
        params.gravity = Gravity.TOP;
        option_Top.setLayoutParams(params);
        addView(option_Top);


        ImageView back_Img = new ImageView(getContext());
        back_Img.setBackgroundResource(R.drawable.back_img);
        back_Img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        params = new FrameLayout.LayoutParams((int) (20 * metrics.density), (int) (20 * metrics.density));
        params.gravity = Gravity.CENTER;
        back_Img.setLayoutParams(params);

        FrameLayout back_ActionView = new FrameLayout(getContext());
        lParams = new LinearLayout.LayoutParams((int) (25 * metrics.density), LayoutParams.MATCH_PARENT);
        lParams.gravity = Gravity.CENTER_VERTICAL;
        back_ActionView.setLayoutParams(lParams);
        back_ActionView.addView(back_Img);
        back_ActionView.setTag(1);
        back_ActionView.setOnClickListener(onClickListener);

        option_Top.addView(back_ActionView);

        // 底部控制条
        option_Botton = new FrameLayout(getContext());
        option_Botton.setBackgroundColor(Color.argb(80, 43, 43, 43));
        params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (30 * metrics.density));
        params.gravity = Gravity.BOTTOM;
        option_Botton.setLayoutParams(params);
        addView(option_Botton);


        ImageView zoom_Img = new ImageView(getContext());
        zoom_Img.setBackgroundResource(R.drawable.zoom);
        params = new LayoutParams((int) (30 * metrics.density), (int) (30 * metrics.density));
        params.gravity = Gravity.CENTER;
        zoom_Img.setLayoutParams(params);
        zoom_Img.setPadding((int) (10 * metrics.density), (int) (5 * metrics.density), (int) (10 * metrics.density), (int) (5 * metrics.density));

        FrameLayout zoom_ActionView = new FrameLayout(getContext());
        params = new LayoutParams((int) (40 * metrics.density), LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.RIGHT;
        zoom_ActionView.setLayoutParams(params);
        zoom_ActionView.setTag(2);
        zoom_ActionView.setOnClickListener(onClickListener);

        zoom_ActionView.addView(zoom_Img);
        option_Botton.addView(zoom_ActionView);

    }

    public void setViewControllerListener(ControlPlayView.ViewControllerListener viewControllerListener){
        this.viewControllerListener = viewControllerListener;
    }

    public void setPlayStatus (PLAY_STATUS play_status) {
        this.play_status = play_status;
        if (PLAY_STATUS.CACHING == play_status) {
            bg_img.setVisibility(View.VISIBLE);
            hit_Layout.setVisibility(View.VISIBLE);

            if(loadingAnimation == null){
                loadingAnimation = new  RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                loadingAnimation.setDuration(800);
                loadingAnimation.setInterpolator(new LinearInterpolator());
                loadingAnimation.setAnimationListener(animationListener);
                loading_img.setAnimation(loadingAnimation);
            }

            if(!animationRunning){
                loadingAnimation.start();
                animationRunning = true;
            }

        } else if (PLAY_STATUS.PLAYING == play_status) {
            animationRunning = false;
            bg_img.setVisibility(View.GONE);
            hit_Layout.setVisibility(View.GONE);
        } else if (PLAY_STATUS.STOP == play_status) {
            bg_img.setVisibility(View.VISIBLE);
            hit_Layout.setVisibility(View.VISIBLE);
        } else if (PLAY_STATUS.ERROR == play_status) {
            bg_img.setVisibility(View.VISIBLE);
            hit_Layout.setVisibility(View.VISIBLE);
        }
    }

    public boolean isShowOption(){
        return showControl;
    }

    private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart (Animation animation) {

        }

        @Override
        public void onAnimationEnd (Animation animation) {
            if(animationRunning){
                loadingAnimation.reset();
                loadingAnimation.start();
            }
        }

        @Override
        public void onAnimationRepeat (Animation animation) {

        }
    };

    @Override
    public boolean onTouchEvent (MotionEvent event) {
        showControl = !showControl;

        if(viewControllerListener!= null)
            viewControllerListener.optionLayoutChange();

        if (showControl) {
            option_Top.setVisibility(View.VISIBLE);
            option_Botton.setVisibility(View.VISIBLE);
        } else {
            option_Top.setVisibility(View.GONE);
            option_Botton.setVisibility(View.GONE);
        }

        return super.onTouchEvent(event);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick (View v) {
            int id = (int) v.getTag();
            switch (id){
                case 1:
                    if(viewControllerListener != null){
                        viewControllerListener.onBack();
                    }
                    break;
                case 2:
                    if(viewControllerListener != null){
                        viewControllerListener.onZoom();
                    }
                    break;
            }
        }
    };



    public static interface  ViewControllerListener{
        public void optionLayoutChange();
        public void onBack();
        public void onZoom();
    }

}
